package cn.openea.perpartner.componet.web.entity.dto;

import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.utils.StringUtils;
import cn.openea.perpartner.componet.web.entity.SmsHistory;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.function.Function;

/**
 * @author ysk
 * @version 1.0.1
 * @Description TODO
 * @createTime 2023年04月11日 17:29:00
 */
@Component
public class SmsHistoryDTOFactory {

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    public static class HistoryDTO {
        private Long id;
        // 发送者
        private String sendTo;
        // 短信内容
        private String smsContent;
        // 用户id
        private String userId;
        // 消息模板内容
        private String smsBody;
        // 接受手机号
        private String phoneNumber;
        // 短信签名
        private String signName;
        // 状态 0 失败 1成功
        private String sendFlag;
        // 上行短信扩展码
        private String smsUpExtendCode;
        // 外部流水扩展字段
        private String outId;
        // 模板编号
        private String templateCode;
        // 模板参数
        private String templateParam;
        // 回执id
        private String bizId;
        // 返回状态码
        private String responseCode;
        // 返回消息
        private String responseMsg;
        // 创建时间
        private String createTime;
    }

    public Function<SmsHistory, HistoryDTO> toHistoryDTO = (history) -> {
        if (history == null) {
            return null;
        }
        HistoryDTO build = HistoryDTO.builder()
                .id(history.getId())
                .sendTo(history.getSendTo())
                .userId(history.getUserId())
                .smsBody(history.getSmsBody())
                .phoneNumber(history.getPhoneNumber())
                .signName(history.getSignName())
                .sendFlag(history.getSendFlag())
                .smsUpExtendCode(history.getSmsUpExtendCode())
                .outId(history.getOutId())
                .templateCode(history.getTemplateCode())
                .templateParam(history.getTemplateParam())
                .bizId(history.getBizId())
                .responseCode(history.getResponseCode())
                .responseMsg(history.getResponseMsg())
                .createTime(history.getCreateTime())
                .smsContent(StringUtils.replaceTextWithJson(history.getSmsBody(), history.getTemplateParam()))
                .build();
        // 设置短信内容

        return build;
    };

}
